package com.bcinfo.iccard.util;


import java.util.HashMap;

public class CodeUtil {
	private static HashMap<String,String> types = new HashMap<String,String>();

	private static boolean initialized = false;

	public CodeUtil() {
		initTypes();
	}

	public static void initTypes() {
		types.put("ez", "application/andrew-inset");
		types.put("hqx", "application/mac-binhex40");
		types.put("cpt", "application/mac-compactpro");
		types.put("doc", "application/msword");
		types.put("bin", "application/octet-stream");
		types.put("dms", "application/octet-stream");
		types.put("lha", "application/octet-stream");
		types.put("lzh", "application/octet-stream");
		types.put("exe", "application/octet-stream");
		types.put("class", "application/octet-stream");
		types.put("so", "application/octet-stream");
		types.put("dll", "application/octet-stream");
		types.put("oda", "application/oda");
		types.put("pdf", "application/pdf");
		types.put("ai", "application/postscript");
		types.put("eps", "application/postscript");
		types.put("ps", "application/postscript");
		types.put("smi", "application/smil");
		types.put("smil", "application/smil");
		types.put("mif", "application/vnd.mif");
		types.put("xls", "application/vnd.ms-excel");
		types.put("xlsx", "application/vnd.ms-excel");
		types.put("ppt", "application/vnd.ms-powerpoint");
		types.put("wbxml", "application/vnd.wap.wbxml");
		types.put("wmlc", "application/vnd.wap.wmlc");
		types.put("wmlsc", "application/vnd.wap.wmlscriptc");
		types.put("bcpio", "application/x-bcpio");
		types.put("vcd", "application/x-cdlink");
		types.put("pgn", "application/x-chess-pgn");
		types.put("cpio", "application/x-cpio");
		types.put("csh", "application/x-csh");
		types.put("dcr", "application/x-director");
		types.put("dir", "application/x-director");
		types.put("dxr", "application/x-director");
		types.put("dvi", "application/x-dvi");
		types.put("spl", "application/x-futuresplash");
		types.put("gtar", "application/x-gtar");
		types.put("hdf", "application/x-hdf");
		types.put("js", "application/x-javascript");
		types.put("skp", "application/x-koan");
		types.put("skd", "application/x-koan");
		types.put("skt", "application/x-koan");
		types.put("skm", "application/x-koan");
		types.put("latex", "application/x-latex");
		types.put("nc", "application/x-netcdf");
		types.put("cdf", "application/x-netcdf");
		types.put("sh", "application/x-sh");
		types.put("shar", "application/x-shar");
		types.put("swf", "application/x-shockwave-flash");
		types.put("sit", "application/x-stuffit");
		types.put("sv4cpio", "application/x-sv4cpio");
		types.put("sv4crc", "application/x-sv4crc");
		types.put("tar", "application/x-tar");
		types.put("tcl", "application/x-tcl");
		types.put("tex", "application/x-tex");
		types.put("texinfo", "application/x-texinfo");
		types.put("texi", "application/x-texinfo");
		types.put("t", "application/x-troff");
		types.put("tr", "application/x-troff");
		types.put("roff", "application/x-troff");
		types.put("man", "application/x-troff-man");
		types.put("me", "application/x-troff-me");
		types.put("ms", "application/x-troff-ms");
		types.put("ustar", "application/x-ustar");
		types.put("src", "application/x-wais-source");
		types.put("xhtml", "application/xhtml+xml");
		types.put("xht", "application/xhtml+xml");
		types.put("zip", "application/zip");
		types.put("rar", "application/rar");
		types.put("au", "audio/basic");
		types.put("snd", "audio/basic");
		types.put("mid", "audio/midi");
		types.put("midi", "audio/midi");
		types.put("kar", "audio/midi");
		types.put("mpga", "audio/mpeg");
		types.put("mp2", "audio/mpeg");
		types.put("mp3", "audio/mpeg");
		types.put("aif", "audio/x-aiff");
		types.put("aiff", "audio/x-aiff");
		types.put("aifc", "audio/x-aiff");
		types.put("m3u", "audio/x-mpegurl");
		types.put("ram", "audio/x-pn-realaudio");
		types.put("rm", "audio/x-pn-realaudio");
		types.put("rpm", "audio/x-pn-realaudio-plugin");
		types.put("ra", "audio/x-realaudio");
		types.put("wav", "audio/x-wav");
		types.put("pdb", "chemical/x-pdb");
		types.put("xyz", "chemical/x-xyz");
		types.put("bmp", "image/bmp");
		types.put("gif", "image/gif");
		types.put("ief", "image/ief");
		types.put("jpeg", "image/jpeg");
		types.put("jpg", "image/jpeg");
		types.put("jpe", "image/jpeg");
		types.put("png", "image/png");
		types.put("tiff", "image/tiff");
		types.put("tif", "image/tiff");
		types.put("djvu", "image/vnd.djvu");
		types.put("djv", "image/vnd.djvu");
		types.put("wbmp", "image/vnd.wap.wbmp");
		types.put("ras", "image/x-cmu-raster");
		types.put("pnm", "image/x-portable-anymap");
		types.put("pbm", "image/x-portable-bitmap");
		types.put("pgm", "image/x-portable-graymap");
		types.put("ppm", "image/x-portable-pixmap");
		types.put("rgb", "image/x-rgb");
		types.put("xbm", "image/x-xbitmap");
		types.put("xpm", "image/x-xpixmap");
		types.put("xwd", "image/x-xwindowdump");
		types.put("igs", "model/iges");
		types.put("iges", "model/iges");
		types.put("msh", "model/mesh");
		types.put("mesh", "model/mesh");
		types.put("silo", "model/mesh");
		types.put("wrl", "model/vrml");
		types.put("vrml", "model/vrml");
		types.put("css", "text/css");
		types.put("html", "text/html");
		types.put("htm", "text/html");
		types.put("asc", "text/plain");
		types.put("txt", "text/plain");
		types.put("rtx", "text/richtext");
		types.put("rtf", "text/rtf");
		types.put("sgml", "text/sgml");
		types.put("sgm", "text/sgml");
		types.put("tsv", "text/tab-separated-values");
		types.put("wml", "text/vnd.wap.wml");
		types.put("wmls", "text/vnd.wap.wmlscript");
		types.put("etx", "text/x-setext");
		types.put("xsl", "text/xml");
		types.put("xml", "text/xml");
		types.put("mpeg", "video/mpeg");
		types.put("mpg", "video/mpeg");
		types.put("mpe", "video/mpeg");
		types.put("qt", "video/quicktime");
		types.put("mov", "video/quicktime");
		types.put("mxu", "video/vnd.mpegurl");
		types.put("avi", "video/x-msvideo");
		types.put("movie", "video/x-sgi-movie");
		types.put("ice", "x-conference/x-cooltalk");
		initialized = true;
	}

	public static String ext2ct(String fileExt) {
		if (!initialized)
			initTypes();
		String ext = fileExt.trim().toLowerCase();
		String type = String.valueOf(types.get(ext));
		if (type == null || type.equals("null"))
			type = "application/x-download";
		System.out.println(ext + "|" + type);
		return type;
	}

	public static int ext2Type(String ext) {
		String exts[] = { "txt", "gif", "jpg", "png", "tiff", "wav", "mid",
				"amr" };
		int s = 0;
		int i = 0;
		do {
			if (i >= exts.length)
				break;
			if (exts[i].equals(ext)) {
				s = i;
				break;
			}
			i++;
		} while (true);
		if (s < 1)
			return 0;
		return s >= 5 ? 2 : 1;
	}

	public static String ct2ext(String contentType) {
		String ct = contentType.toLowerCase();
		if (ct.indexOf("gif") != -1)
			return "gif";
		if (ct.indexOf("jpg") != -1 || ct.indexOf("jpeg") != -1)
			return "jpg";
		if (ct.indexOf("text") != -1)
			return "txt";
		if (ct.indexOf("amr") != -1)
			return "amr";
		if (ct.indexOf("mid") != -1)
			return "mid";
		if (ct.indexOf("wav") != -1)
			return "wav";
		if (ct.indexOf("smil") != -1)
			return "smil";
		else
			return "";
	}

	public static String getCharsetFromContentType(String contentType) {
		String ct = contentType.toLowerCase();
		int p = ct.indexOf("charset=");
		if (p == -1)
			return null;
		int n = ct.indexOf(";", p);
		if (n == -1)
			n = ct.length();
		return ct.substring(p, n);
	}
}
